package ext.tianma.forms.processors;

import java.util.Calendar;

import wt.change2.WTChangeActivity2Master;
import wt.change2.WTChangeActivity2MasterIdentity;
import wt.change2.WTChangeOrder2Master;
import wt.change2.WTChangeOrder2MasterIdentity;
import wt.change2.WTChangeRequest2Master;
import wt.change2.WTChangeRequest2MasterIdentity;
import wt.fc.IdentificationObject;
import wt.fc.Identified;
import wt.fc.IdentityHelper;
import wt.fc.Persistable;
import wt.org.WTPrincipal;
import wt.session.SessionHelper;
import wt.util.WTException;
import wt.vc.Iterated;
import wt.vc.Mastered;

public class ChangeNumberUtil {
public static void updateChangeItemNumber(Iterated iterated) {
		
		if( iterated == null ){
			return ;
		}
		
		Persistable persistable =  iterated.getMaster();
		
		if( ( persistable != null ) && ( persistable instanceof Identified ) ){
			
			Identified identified = ( Identified ) persistable ;

			try {
				IdentificationObject masteridentity = identified.getIdentificationObject();
				
				if( masteridentity instanceof WTChangeOrder2MasterIdentity ){
					WTChangeOrder2MasterIdentity ecaMaterIdentity = ( WTChangeOrder2MasterIdentity ) masteridentity ;
					
					setChangeECNNumber( (Mastered) persistable , ecaMaterIdentity ) ;
					
				}else if( masteridentity instanceof WTChangeRequest2MasterIdentity ){
					WTChangeRequest2MasterIdentity wtChangeRequest2MasterIdentity =( WTChangeRequest2MasterIdentity )masteridentity;
					
					setChangeECRNumber( (Mastered) persistable , wtChangeRequest2MasterIdentity ) ;
					
				}else if( masteridentity instanceof WTChangeActivity2MasterIdentity ){
					WTChangeActivity2MasterIdentity wtChangeActivity2MasterIdentity =(WTChangeActivity2MasterIdentity)masteridentity;
					
					setChangeECANumber((Mastered) persistable, wtChangeActivity2MasterIdentity);
				}
				
			} catch (WTException e) {
				e.printStackTrace();
			}finally{
				
			}
			
		}		
		
	}
	
	private static void setChangeECNNumber( Mastered mastered , WTChangeOrder2MasterIdentity masteridentity) {
		Calendar calendar=Calendar.getInstance();//获得系统当前日期
		int year = calendar.get(Calendar.YEAR);
		int moth = calendar.get(Calendar.MONTH)+1;
		
		if( mastered instanceof WTChangeOrder2Master ){
			WTChangeOrder2Master wtchangeOrder2Master = ( WTChangeOrder2Master ) mastered ;
			
			String number = wtchangeOrder2Master.getNumber() ;
			
			if( number.length() <=6 ){
				number = "DCN" + year + moth + number ;
				//boolean flag = SessionServerHelper.manager.setAccessEnforced(false);	
				try {
					WTPrincipal curUser = SessionHelper.manager.getPrincipal();
					SessionHelper.manager.setAdministrator();
					
					masteridentity.setNumber(number);
					IdentityHelper.service.changeIdentity(wtchangeOrder2Master , masteridentity);
					SessionHelper.manager.setPrincipal(curUser.getName());
				} catch (Exception e) {
					e.printStackTrace();
				}	finally{
					
				}		
			}
		}
		
		
	}	

	private static void setChangeECANumber(Mastered mastered , WTChangeActivity2MasterIdentity masteridentity) {
		Calendar calendar=Calendar.getInstance();//获得系统当前日期
		int year = calendar.get(Calendar.YEAR);
		
		if( mastered instanceof WTChangeActivity2Master ){
			WTChangeActivity2Master wtChangeActivity2Master = ( WTChangeActivity2Master ) mastered ;
			
			String number = wtChangeActivity2Master.getNumber() ;
			
			if( number.length() <=6 ){
				number = year + number ;
				//boolean flag = SessionServerHelper.manager.setAccessEnforced(false);
				try {	
					WTPrincipal curUser = SessionHelper.manager.getPrincipal();
					SessionHelper.manager.setAdministrator();
					
					masteridentity.setNumber(number);
					IdentityHelper.service.changeIdentity(wtChangeActivity2Master , masteridentity);
					SessionHelper.manager.setPrincipal(curUser.getName());
				} catch (Exception e) {
					e.printStackTrace();
				}	finally{
					//SessionServerHelper.manager.setAccessEnforced(flag);
				}		
			}
		}
	}

	private static void setChangeECRNumber(Mastered mastered , WTChangeRequest2MasterIdentity masteridentity) {
		Calendar calendar=Calendar.getInstance();//获得系统当前日期
		int year = calendar.get(Calendar.YEAR);
		int moth = calendar.get(Calendar.MONTH)+1;
		
		if( mastered instanceof WTChangeRequest2Master){
			WTChangeRequest2Master wtChangeRequest2Master = ( WTChangeRequest2Master ) mastered ;
			
			String number = wtChangeRequest2Master.getNumber() ;
			
			if( number.length() <=6 ){
				number = "DCR" + year + moth + number ;
				//boolean flag = SessionServerHelper.manager.setAccessEnforced(false);
				try {	
					WTPrincipal curUser = SessionHelper.manager.getPrincipal();
					SessionHelper.manager.setAdministrator();
					
					masteridentity.setNumber(number);
					IdentityHelper.service.changeIdentity( wtChangeRequest2Master , masteridentity);
					SessionHelper.manager.setPrincipal(curUser.getName());
				} catch (Exception e) {
					e.printStackTrace();
				}	finally{
					//SessionServerHelper.manager.setAccessEnforced(flag);
				}		
			}
		}
	}	
}
